package cn.gong.movie.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import z.utils.LogUtils;

import framework.utils.StringUtils;

public class DateUtils {
	private static String[] DAY_31 = new String[31];
	private static String[] DAY_30 = new String[30];
	private static String[] DAY_29 = new String[29];
	private static String[] DAY_28 = new String[28];
	private static String[] MONTH = new String[12];
	private static String[] YEAR = new String[100];
	private static String[] YEAR_TOTAL = new String[200];
	private static String[] NOWMONTHS;
	private static String[] NOWDAYS;

	static {
		for (int i = 0; i < 31; i++) {
			String day = (i + 1) + "日";
			switch (i) {
			default:
				DAY_28[i] = day;
			case 28:
				DAY_29[i] = day;
			case 29:
				DAY_30[i] = day;
			case 30:
				DAY_31[i] = day;
				break;
			}
		}

		for (int i = 0; i < 12; i++) {
			MONTH[i] = (i + 1) + "月";
		}

		Calendar instance = Calendar.getInstance();
		int nowYear = instance.get(Calendar.YEAR);
		for (int i = 0; i < 200; i++) {
			YEAR_TOTAL[i] = (nowYear + i - 99) + "年";
		}

	}

	public static void initYmd() {
		Calendar instance = Calendar.getInstance();

		int nowYear = instance.get(Calendar.YEAR);
		for (int i = 0; i < 100; i++) {
			YEAR[i] = (nowYear + i - 99) + "年";
		}

		int nowMonth = instance.get(Calendar.MONTH);
		NOWMONTHS = new String[nowMonth + 1];
		for (int i = 0; i < NOWMONTHS.length; i++) {
			NOWMONTHS[i] = (i + 1) + "月";
		}

		int nowDay = instance.get(Calendar.DAY_OF_MONTH);
		NOWDAYS = new String[nowDay];
		for (int i = 0; i < NOWDAYS.length; i++) {
			NOWDAYS[i] = (i + 1) + "日";
		}
	}

	public static String[] getYearArray() {
		return YEAR;
	}

	public static String[] getYearArrayTotal() {
		return YEAR_TOTAL;
	}

	public static String[] getMonthArray(int yearPosition) {
		if (yearPosition == YEAR.length - 1) {
			return NOWMONTHS;
		} else {
			return MONTH;
		}
	}

	public static String[] getMonthArrayTotal() {
		return MONTH;
	}

	public static String[] getDayArray(int yearPosition, int monthPosition) {
		if (yearPosition == YEAR.length - 1 && monthPosition == NOWMONTHS.length - 1) {
			return NOWDAYS;
		} else {
			switch (monthPosition + 1) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return DAY_31;
			case 2:
				String yearStr = YEAR[yearPosition];
				int year = Integer.valueOf(yearStr.substring(0, yearStr.length() - 1));
				if (year % 4 == 0) {
					return DAY_29;
				} else {
					return DAY_28;
				}
			default:
				return DAY_30;
			}
		}
	}

	public static String[] getDayArrayTotal(String yearStr, int monthPosition) {
		switch (monthPosition + 1) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return DAY_31;
		case 2:
			int year = Integer.valueOf(yearStr.substring(0, yearStr.length() - 1));
			LogUtils.log("year:"+year);
			if (year % 4 == 0) {
				LogUtils.log("day_29");
				return DAY_29;
			} else {
				LogUtils.log("day_28");
				return DAY_28;
			}
		default:
			return DAY_30;
		}
	}

	private static int test = 0;

	public static String getTitle(String date) {
		if (StringUtils.isNotBlank(date)) {
			if (isToday(date)) {
				return "今天";
			} else if (isTomorrow(date)) {
				return "明天";
			} else if (isThisYear(date)) {
				return getMonth(date) + "月";
			} else {
				return getYear(date) + "年" + getMonth(date) + "月";
			}
		} else {
			return "";
		}

	}

	public static String getDateStr(int year, int month, int day) {
		Calendar instance = Calendar.getInstance();
		instance.set(year, month, day);
		return getDateStr(instance.getTime());
	}

	public static String getDateStr(String year, String month, String day) {
		Calendar instance = Calendar.getInstance();
		instance.set(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
		return getDateStr(instance.getTime());
	}

	public static String getDateStr(Date date) {
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}

	public static String getDateStr(long currentTime) {
		return getDateStr(new Date(currentTime));
	}

	public static String getDateYMD(String birth) {
		return getYear(birth) + "年" + getMonth(birth) + "月" + getDay(birth) + "日";
	}

	public static String getTodayStr() {
		return getDateStr(new Date(System.currentTimeMillis()));
	}

	public static String getTomorrowStr() {
		return getDateStr(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000));
	}

	public static boolean isToday(String date) {
		return date.equals(getTodayStr());
	}

	public static boolean isToday(Date date) {
		return getDateStr(date).equals(getTodayStr());
	}

	private static boolean isTomorrow(String date) {
		return date.equals(getTomorrowStr());
	}

	private static boolean isThisYear(String date) {
		return getYear(date) == getYear(getTodayStr());
	}

	public static String getYm(String str) {
		return getYear(str) + "-" + getMonth(str);
	}

	public static String getYm(Date date) {
		return getYm(getDateStr(date));
	}

	public static int getYear(String str) {
		try {
			return Integer.valueOf(str.substring(0, 4));
		} catch (Exception e) {
			return 0;
		}
	}

	public static int getYear(Date date) {
		return getYear(getDateStr(date));
	}

	public static int getMonth(String str) {
		try {
			return Integer.valueOf(str.substring(4, 6));
		} catch (Exception e) {
			return 1;
		}
	}

	public static int getMonth(Date date) {
		return getMonth(getDateStr(date));
	}

	public static int getDay(String str) {
		try {
			return Integer.valueOf(str.substring(6));
		} catch (Exception e) {
			return 1;
		}
	}

	public static int getDay(Date date) {
		return getDay(getDateStr(date));
	}

	public static int getCurrentYear() {
		return getYear(getTodayStr());
	}

	public static int getCurrentMonth() {
		return getMonth(getTodayStr());
	}

	public static int getCurrentDay() {
		return getDay(getTodayStr());
	}

	public static boolean equals(Date date, Date clickDate) {
		if (date == null || clickDate == null) {
			return false;
		}
		return getDateStr(date).equals(getDateStr(clickDate));
	}

	public static int getAge(String birthday) {
		try {
			return getCurrentYear() - getYear(birthday);
		} catch (Exception e) {
			return 0;
		}
	}
}
